import ajax from './ajax'
import { SessionStorage } from 'quasar'

export default {
  loadCategories (onSuccess) {
    if( SessionStorage.has( 'categories' )){
      onSuccess( SessionStorage.get.item( 'categories' ))
      return 
    }

    ajax.get( '/getCategories' )
      .then( result => {
        let { data, ret } = result.data
        // console.log( 'data::::', data ) 
        if( ret == 0 ){
          onSuccess( data )
          SessionStorage.set( 'categories', data )

        }
    });
  },
  getAccounts( onSuccess ){
      // if( SessionStorage.has( 'accounts' )){
      //     onSuccess( SessionStorage.get.item( 'accounts' ))
      //     return 
      // }
      ajax.get( '/douyin/getAccounts' )
        .then( result => {
          let { data, ret } = result.data
          // console.log( 'data::::', data ) 
          if( ret == 0 ){
            onSuccess( data )
            // collectTags( data )
            // SessionStorage.set( 'accounts', data )
          }
      });

      // onSuccess( result )
  },
  deleteAccount( id, onSuccess ){
    //   /douyin/:id/delete
    ajax.post( `/douyin/${id}/delete` )
        .then( result => {
          let { data, ret } = result.data
          // console.log( 'data::::', data ) 
          if( ret == 200 ){
            onSuccess()
            // collectTags( data )
            // SessionStorage.set( 'accounts', data )
          }
      });
  },
  getVideos( options, onSuccess ){
    let { 
      showFavored = false, 
      accountid, 
      page = 1, 
      limit = 20, 
      days = 0
    } = options
    let url = !!accountid ? 
      `/douyin/${accountid}/videos` : 
      `/douyin/videos`
    ajax.get( url + `?page=${page}&limit=${limit}&collect=${!!showFavored}&days=${days}` )
        .then( result => {
            let { data, ret } = result.data
            // console.log( 'data::::', data ) 
            if( ret == 0 ){
                onSuccess( data )
            }
        });
    // onSuccess()
  },
  
  deleteVideos( id, onSuccess ){
    //   /douyin/:id/delete
    ajax.post( `/douyin/videos/${id}/delete` )
        .then( result => {
          let { data, ret } = result.data
          // console.log( 'data::::', data ) 
          if( ret == 200 ){
            onSuccess()
            // collectTags( data )
            // SessionStorage.set( 'accounts', data )
          }
      });
  },

  getBgms( page=1, onSuccess ){
    // if( SessionStorage.has( 'accounts' )){
    //     onSuccess( SessionStorage.get.item( 'accounts' ))
    //     return 
    // }
    ajax.get( `/douyin/bgms?page=${page}` )
      .then( result => {
        let { data, ret } = result.data
        // console.log( 'data::::', data ) 
        if( ret == 0 ){
          onSuccess( data )
          // collectTags( data )
          // SessionStorage.set( 'accounts', data )
        }
    })
    // onSuccess( result )
  },

  favorVideos( id, collect ) {
    return new Promise( resolve => {
      ajax.post( `/douyin/videos/${id}/mark?collect=${!!collect}` )
        .then( result => {
          let { data, ret } = result.data
          // console.log( 'data::::', data ) 
          if( ret == 200 ){
            // onSuccess( data )
            // collectTags( data )
            // SessionStorage.set( 'accounts', data )
            resolve( data )
            return data 
          }
      })
    })
  },
}